let currentOffset = 0;
let apiBaseUrl = 'https://gae.piaoyu.org/';

function init() {
    // $('.weui-media-box_text').click(function () {
    //     let desc = $(this).find('.weui-media-box__desc');
    //     if (desc.css('-webkit-line-clamp') === '3') {
    //         desc.css('-webkit-line-clamp', '10');
    //         console.log($(this).find('.weui-cell__bd'));
    //         $(this).find('.weui-cell__bd').css('display', '');
    //     }
    //     let imgGallery = $(this).find('.img-gallery').clone();
    //     $(this).append(imgGallery);
    // });
    $('#gallery').click(function () {

    });

    $(window).bind("scroll", function () {
        if ($(document).scrollTop() + $(window).height() >= $(document).height() - 10
            && $('#read-more').css('display') !== 'none') {
            loadNews();
        }
    });

    $('.weui-icon-cancel').click(function () {
        $('#gallery').fadeOut(100);
    });
    loadNews();
}

function addNews(obj) {
    let newsNode = $('#tpl-example .weui-media-box').clone();
    newsNode.find('.weui-media-box__title').text(obj.title);
    newsNode.find('.weui-media-box__desc').append(obj.content).append(
        '&nbsp;<a class="readmore-link" target="_blank" href="https://m.weibo.cn/status/' + obj.weibo_id + '">More</a>');
    newsNode.click(function () {
        let desc = $(this).find('.weui-media-box__desc');
        if (desc.css('-webkit-line-clamp') === '3') {
            desc.css('-webkit-line-clamp', '10');
            let $parent = $(this);
            if (obj.media_info && obj.media_info !== '') {
                // 视频数据
                $('.container .media-play').remove();
                let $mediaLoading = $('#tpl-example .media-loading').clone();
                $parent.append($mediaLoading);
                $.ajax({
                    url: apiBaseUrl + 'get_page_info',
                    data: {weibo_id: obj.weibo_id},
                    type: 'GET',
                    dataType: 'json',
                    error: function (xhr) {
                        console.log('加载失败：', xhr);
                    },
                    success: function (xhr) {
                        console.log(xhr);
                        $mediaLoading.remove();
                        let $mediaPlay = $('#tpl-example .media-play').clone();
                        $mediaPlay.find('source').attr('src', xhr.media_info.stream_url);
                        $parent.append($mediaPlay);
                    }
                });
            }
            if (obj.pics_json && obj.pics_json !== '') {
                console.log(JSON.parse(obj.pics_json));
                let imgGallery = $('#tpl-example .img-gallery').clone();
                let pics = JSON.parse(obj.pics_json);
                for (let i = 0; i < pics.length; i++) {
                    let $li = $('<li class="weui-uploader__file"></li>');
                    $li.css('background-image', 'url(' + pics[i].url + ')');
                    $li.data('pics', pics[i]);
                    imgGallery.find('#weiboPics').append($li);
                }
                imgGallery.find('.weui-uploader__file').click(function () {
                    // $('#galleryImg').css('background-image', 'url(' + $(this).data('pics').large.url + ')');
                    // $('#gallery').fadeIn(100);
                    $('.container .img-large-preview').remove();
                    let $imgLargePreview = $('#tpl-example .img-large-preview').clone();
                    $imgLargePreview.find('img').attr('src', $(this).data('pics').large.url);
                    $imgLargePreview.click(function () {
                        $(this).remove();
                    });
                    $(this).after($imgLargePreview);
                });
                $parent.append(imgGallery);
            }
        }
    });
    $('.weui-panel__bd').append(newsNode);
}

function loadNews() {
    $('.weui-loadmore').css('display', '');
    $('#read-more').css('display', 'none');
    $.ajax({
        async: true,
        url: apiBaseUrl + 'get_news',
        data: {offset: currentOffset, limit: 10},
        type: 'GET',
        dataType: 'html',
        error: function (xhr) {
            console.log('加载失败：', xhr);
        },
        success: function (json) {
            currentOffset += 10;
            let newsList = JSON.parse(json).data;
            for (let i = 0; i < newsList.length; i++) {
                addNews(newsList[i]);
            }
            $('.weui-loadmore').css('display', 'none');
            $('#read-more').css('display', '');
        }
    });
}